package com.wzm.testcase;

import com.wzm.entity.WmsTask;
import com.wzm.mapper.WmsTaskMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class TestMybatis {

    public static void TestMybatis() throws IOException {
        // 创建SqlSessionFactory
//        String resource = "D:\\JavaProject\\java-interface-automation\\src\\main\\java\\com\\junan\\mapper\\xml\\MybatisConfig.xml";
        String resource="MybatisConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 获取SqlSession
        try (SqlSession session = sqlSessionFactory.openSession()) {
            try {
                // 获取Mapper实例
                WmsTaskMapper wmsTaskMapper = session.getMapper(WmsTaskMapper.class);
                // 调用Mapper方法进行数据库操作
                WmsTask wmsTask = new WmsTask();
                WmsTask w = wmsTaskMapper.getTaskBySn("11");
                session.commit();

                System.out.println(w.getTaskNo());
            }catch (Exception e){
                session.rollback();
            }
        }
    }

    public static void main(String[] args) {
        try {
            TestMybatis();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
